﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Xml;

namespace AIC_CD
{
    public partial class BaoCaoXepLoaiCongDoanCoSo_Update : MyFromBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblNotice.Visible = false;

            CheckPermission();
            if (!IsPostBack)
            {
                Config();
                BindDataToControls();
            }



        }

        /// <summary>
        /// Thủ tục kiểm tra quyền sử dụng của người dùng có được quyền truy cập vào mục này hay không?
        /// </summary>
        private void CheckPermission()
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission("BaoCaoXepLoaiCongDoanCoSo");
            if (Role == null || Role["Xem"] == 0 || ((Role["Xem"] == 1 && Role["Sua"] == 0)))
            {
                Response.Redirect("Index.aspx?p=0");
            }
        }

        /// <summary>
        /// Thủ tục cấu hình giao diện, các controls
        /// </summary>
        private void Config()
        {
            //TODO
            txtNguoiLapBaoCao.Text = "";
            txtThuTruongCoQuanChinhTri.Text = "";

        }

        /// <summary>
        /// Bind Data vào các contrls như Combobox, ListBox, textBox ...
        /// </summary>
        private void BindDataToControls()
        {
            //TODO
            if (Session["userid"] != null)
            {

                string sql = "Select DV.* " +
                             "From tblDonVi DV " +
                             "Inner Join tblUser U On U.DonViID=DV.ID " +
                             "Where U.ID=" + Session["userid"];
                DataTable dtDV = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
                tdTenDonVi.InnerText = "(" + dtDV.Rows[0]["MaDV"] + ") " + dtDV.Rows[0]["TenDV"];

                sql = "Select ID,TenDM From tblDanhMuc Where MaDM='" + MyConst.DanhMuc.KyBaoCao.Ma + "' ";
                SqlDataReader reKyBaoCao = SqlHelper.ExecuteReader(CommandType.Text, sql);
                clsDataBind.CreateCombo("Tất cả", reKyBaoCao, cboKyBaoCao, 1, 0);


            }


        }

        /// <summary>
        /// Bind Data vào gridview
        /// </summary>
        private void BindDataToGrid()
        {
            //TODO

            string sql = "Select BC.* From tblBaoCaoXepLoaiCongDoanCoSo BC " +
                            "Inner Join tblDonVi DV On DV.ID=BC.DonViID " +
                            "Inner Join tblUser U On U.DonViID=DV.ID " +
                            "Where BC.IsCommon<>1 And U.ID=" + Session["userid"] + " And BC.KyBaoCaoID_DM=" + cboKyBaoCao.SelectedValue;
            grvData.DataSource = SqlHelper.ExecuteDataTable(System.Data.CommandType.Text, sql);
            grvData.DataBind();

            sql = "Select BC.* From tblBaoCaoXepLoaiCongDoanCoSo BC " +
                         "Inner Join tblDonVi DV On DV.ID=BC.DonViID " +
                         "Inner Join tblUser U On U.DonViID=DV.ID " +
                         "Where BC.IsCommon=1 And U.ID=" + Session["userid"] + " And BC.KyBaoCaoID_DM=" + cboKyBaoCao.SelectedValue;
            DataTable temDT = SqlHelper.ExecuteDataTable(System.Data.CommandType.Text, sql);
            if (temDT.Rows.Count > 0)
            {
                txtTenBaoCao.Text = temDT.Rows[0]["CM_TenBaoCao"].ToString();
                txtNguoiLapBaoCao.Text = temDT.Rows[0]["CM_NguoiLapBieu"].ToString();
                txtThuTruongCoQuanChinhTri.Text = temDT.Rows[0]["CM_ThuTruong"].ToString();
            }

            btnLuu.Visible = true;


        }

        #region Events

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Index.aspx");
        }

        protected void cboKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            grvData.EditIndex = -1;
            BindDataToGrid();

            if (((DataTable)grvData.DataSource).Rows.Count == 0)
            {
                SqlTransaction MyTran = SqlHelper.BeginTransaction();
                try
                {
                    DataTable temDT = SqlHelper.ExecuteDataTable(CommandType.Text, "Select * From tblDanhMuc Where MaDM='" + MyConst.DanhMuc.NoiDungMau1.Ma + "' ");
                    List<SqlParameter> lstParameter = new List<SqlParameter>();

                    string sSQL = "";
                    string sDVID = MyCommon.GetDonViID(Session["userid"].ToString());
                    foreach (DataRow dr in temDT.Rows)
                    {
                        lstParameter.Clear();

                        lstParameter.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.NVarChar, ParameterDirection.Input, sDVID));
                        lstParameter.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.NVarChar, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
                        lstParameter.Add(SqlHelper.CreateParameterObject("@NoiDungMau1ID_DM", SqlDbType.Int, ParameterDirection.Input, dr["ID"]));
                        lstParameter.Add(SqlHelper.CreateParameterObject("@NoiDung", SqlDbType.NVarChar, ParameterDirection.Input, dr["TenDM"]));
                        lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));


                        sSQL = "Insert Into tblBaoCaoXepLoaiCongDoanCoSo(DonViID, KyBaoCaoID_DM, NoiDungMau1ID_DM, NoiDung, CreateDate, UpdateDate, UserID, IsCommon) " +
                                "Values(@DonViID, @KyBaoCaoID_DM, @NoiDungMau1ID_DM, @NoiDung, GetDate(), GetDate(), @UserID,0)";
                        SqlHelper.ExecuteNonQuery(MyTran, CommandType.Text, sSQL, lstParameter);
                    }

                    lstParameter.Clear();

                    lstParameter.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.NVarChar, ParameterDirection.Input, sDVID));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@KyBaoCaoID_DM", SqlDbType.NVarChar, ParameterDirection.Input, cboKyBaoCao.SelectedValue));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@NoiDungMau1ID_DM", SqlDbType.Int, ParameterDirection.Input, 0));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@NoiDung", SqlDbType.NVarChar, ParameterDirection.Input, ""));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@CM_TenBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, "BÁO CÁO XẾP LOẠI CÔNG ĐOÀN CƠ SỞ NĂM 20.."));



                    sSQL = "Insert Into tblBaoCaoXepLoaiCongDoanCoSo(DonViID, KyBaoCaoID_DM, NoiDungMau1ID_DM, NoiDung, CreateDate, UpdateDate, UserID,CM_TenBaoCao, IsCommon) " +
                            "Values(@DonViID, @KyBaoCaoID_DM, @NoiDungMau1ID_DM, @NoiDung, GetDate(), GetDate(), @UserID,@CM_TenBaoCao,1)";
                    SqlHelper.ExecuteNonQuery(MyTran, CommandType.Text, sSQL, lstParameter);

                    MyTran.Commit();
                }
                catch
                {
                    MyTran.Rollback();
                }
                grvData.EditIndex = -1;
                BindDataToGrid();
            }

        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {

            string sSQL = "Update tblBaoCaoXepLoaiCongDoanCoSo Set  CM_NguoiLapBieu=@CM_NguoiLapBieu, CM_ThuTruong=@CM_ThuTruong, CM_TenBaoCao=@CM_TenBaoCao " +
                            "Where IsCommon=1 And DonViID=" + MyCommon.GetDonViID(Session["userid"].ToString()) + " And KyBaoCaoID_DM=" + cboKyBaoCao.SelectedValue;

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_NguoiLapBieu", SqlDbType.NVarChar, ParameterDirection.Input, txtNguoiLapBaoCao.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_ThuTruong", SqlDbType.NVarChar, ParameterDirection.Input, txtThuTruongCoQuanChinhTri.Text.Trim()));
            lstParameter.Add(SqlHelper.CreateParameterObject("@CM_TenBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, txtTenBaoCao.Text.Trim()));


            SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);
            MyCommon.AddMessage(this.Master, "Cập nhật báo cáo thành công !", MyConst.MessageType.Success);

        }

        #endregion Events


        #region GridCommand

        private static void AddMergedCells(GridViewRow objgridviewrow, TableCell objtablecell, int colspan, int rowspan, string celltext)
        {
            objtablecell = new TableCell();
            objtablecell.Text = celltext;
            objtablecell.ColumnSpan = colspan;
            objtablecell.RowSpan = rowspan;
            objtablecell.HorizontalAlign = HorizontalAlign.Center;
            objgridviewrow.Cells.Add(objtablecell);
        }

        protected void grvData_RowCreated(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.Header)
            {

                GridView objGridView = (GridView)sender;
                //GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow2 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow3 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow4 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                TableCell objtablecell = new TableCell();
                AddMergedCells(objgridviewrow, objtablecell, 1, 3, "Nội dung");


                AddMergedCells(objgridviewrow, objtablecell, 1, 3, "Tổng số CDCS");

                AddMergedCells(objgridviewrow, objtablecell, 2, 1, "Trong đó");

                AddMergedCells(objgridviewrow, objtablecell, 10, 1, "Xếp loại công đoàn cơ sở");
                AddMergedCells(objgridviewrow, objtablecell, 1, 3, "Ghi chú");
                AddMergedCells(objgridviewrow, objtablecell, 1, 3, "Sửa");
                objgridviewrow.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);

                objGridView.Controls[0].Controls.AddAt(0, objgridviewrow);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Đủ ĐK xếp loại ");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 2, "Không đủ ĐK xếp loại");



                AddMergedCells(objgridviewrow2, objtablecell, 2, 1, "Đạt vững mạnh xuất xắc");
                AddMergedCells(objgridviewrow2, objtablecell, 2, 1, "Đạt vững mạnh khá");
                AddMergedCells(objgridviewrow2, objtablecell, 2, 1, "Đạt vững mạnh trung bình");
                AddMergedCells(objgridviewrow2, objtablecell, 2, 1, "Yếu kém (Không đạt vững mạnh)");
                AddMergedCells(objgridviewrow2, objtablecell, 2, 1, "Không xếp loại");
                objgridviewrow2.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objGridView.Controls[0].Controls.AddAt(1, objgridviewrow2);
                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Số lượng CĐCS");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Tỷ lệ %");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Số lượng CĐCS");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Tỷ lệ %");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Số lượng CĐCS");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Tỷ lệ %");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Số lượng CĐCS");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Tỷ lệ %");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Số lượng CĐCS");
                AddMergedCells(objgridviewrow3, objtablecell, 1, 1, "Tỷ lệ %");
                objgridviewrow3.BackColor = System.Drawing.Color.FromArgb(0x10, 0x9A, 0xEA);
                objGridView.Controls[0].Controls.AddAt(2, objgridviewrow3);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "1");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "2");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "3");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "4");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "5");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "6");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "7");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "8");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "9");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "10");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "11");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "12");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "13");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "14");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "15");
                AddMergedCells(objgridviewrow4, objtablecell, 1, 1, "16");

                objgridviewrow4.BackColor = System.Drawing.Color.FromArgb(0xC7, 0xF4, 0x9C);
                objGridView.Controls[0].Controls.AddAt(3, objgridviewrow4);
            }
        }

        protected void grvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {


            RowDataBound(sender, e);
        }

        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sID = e.CommandArgument.ToString().Trim();
            string sCommandName = e.CommandName;


            if ((sCommandName == AIC_CD.MyConst.Action.Approve))
            {
                SqlTransaction MyTrans = SqlHelper.BeginTransaction();
                try
                {
                    // cap nhat vao database
                    TextBox txtF2 = (TextBox)grvData.Rows[grvData.EditIndex].Cells[1].Controls[0];
                    TextBox txtF3 = (TextBox)grvData.Rows[grvData.EditIndex].Cells[2].Controls[0];
                    TextBox txtF4 = (TextBox)grvData.Rows[grvData.EditIndex].Cells[3].Controls[0];
                    TextBox txtF5 = (TextBox)grvData.Rows[grvData.EditIndex].Cells[4].Controls[0];
                    TextBox txtF6 = (TextBox)grvData.Rows[grvData.EditIndex].Cells[5].Controls[0];
                    TextBox txtF7 = (TextBox)grvData.Rows[grvData.EditIndex].Cells[6].Controls[0];
                    TextBox txtF8 = (TextBox)grvData.Rows[grvData.EditIndex].Cells[7].Controls[0];
                    TextBox txtF9 = (TextBox)grvData.Rows[grvData.EditIndex].Cells[8].Controls[0];
                    TextBox txtF10 = (TextBox)grvData.Rows[grvData.EditIndex].Cells[9].Controls[0];
                    TextBox txtF11 = (TextBox)grvData.Rows[grvData.EditIndex].Cells[10].Controls[0];
                    TextBox txtF12 = (TextBox)grvData.Rows[grvData.EditIndex].Cells[11].Controls[0];
                    TextBox txtF13 = (TextBox)grvData.Rows[grvData.EditIndex].Cells[12].Controls[0];
                    TextBox txtF14 = (TextBox)grvData.Rows[grvData.EditIndex].Cells[13].Controls[0];
                    TextBox txtGhiChu = (TextBox)grvData.Rows[grvData.EditIndex].Cells[14].Controls[0];

                    string sSQL = "Update tblBaoCaoXepLoaiCongDoanCoSo Set F2=@F2, F3=@F3, F4=@F4, F5=@F5, F6=@F6, F7=@F7, F8=@F8, F9=@F9, F10=@F10, F11=@F11, F12=@F12, F13=@F13, F14=@F14, Ghichu=@GhiChu, UpdateDate=GetDate(), UserID=@UserID Where ID=@ID ";

                    List<SqlParameter> lstParameter = new List<SqlParameter>();
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F2", SqlDbType.Float, ParameterDirection.Input, txtF2.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F3", SqlDbType.Float, ParameterDirection.Input, txtF3.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F4", SqlDbType.Float, ParameterDirection.Input, txtF4.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F5", SqlDbType.Float, ParameterDirection.Input, txtF5.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F6", SqlDbType.Float, ParameterDirection.Input, txtF6.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F7", SqlDbType.Float, ParameterDirection.Input, txtF7.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F8", SqlDbType.Float, ParameterDirection.Input, txtF8.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F9", SqlDbType.Float, ParameterDirection.Input, txtF9.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F10", SqlDbType.Float, ParameterDirection.Input, txtF10.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F11", SqlDbType.Float, ParameterDirection.Input, txtF11.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F12", SqlDbType.Float, ParameterDirection.Input, txtF12.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F13", SqlDbType.Float, ParameterDirection.Input, txtF13.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@F14", SqlDbType.Float, ParameterDirection.Input, txtF14.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@UserID", SqlDbType.Int, ParameterDirection.Input, Session["userid"]));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@GhiChu", SqlDbType.NVarChar, ParameterDirection.Input, txtGhiChu.Text.Trim()));
                    lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, sID));
                    SqlHelper.ExecuteNonQuery(CommandType.Text, sSQL, lstParameter);
                    MyTrans.Commit();
                    grvData.EditIndex = -1;
                    BindDataToGrid();
                    MyCommon.AddMessage(this.Page.Master, "Cập nhật thông tin thành công", MyConst.MessageType.Success);
                }
                catch (Exception)
                {
                    MyCommon.AddMessage(this.Page.Master, "Có lỗi trong dữ liệu nhập vào, các trường số liệu bắt buộc phải là một con số !", MyConst.MessageType.Warning);
                    MyTrans.Rollback();
                }
            }
        }

        protected void grvData_RowEditing(object sender, GridViewEditEventArgs e)
        {

            grvData.EditIndex = e.NewEditIndex;
            BindDataToGrid();

            TextBox txtF1 = (TextBox)grvData.Rows[grvData.EditIndex].Cells[0].Controls[0];
            txtF1.Width = 200;
            txtF1.Enabled = false;

            TextBox txtF15 = (TextBox)grvData.Rows[grvData.EditIndex].Cells[14].Controls[0];
            txtF15.Width = 300;




        }



        protected void grvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //grvData.PageIndex = e.NewPageIndex;
            //BindDataToGrid();
        }


        protected void grvData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvData.EditIndex = -1;
            BindDataToGrid();
        }

        #endregion GridCommand




    }
}
